﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Translator.GoogleTranslateApi;

namespace WcfTranslateLibrary
{
	[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
	public class TranslateService : ITranslateService
	{
		GoogleTranslateApiImplementation translateApi = null;
		public TranslateService()
		{
			translateApi = new GoogleTranslateApiImplementation();
		}

		public TranslationResponse Translate(TranslationRequest request)
		{
			try
			{
				translateApi.SetSourceLanguage(request.FromLanguage);
				translateApi.SetTranslateLanguage(request.ToLanguage);
				GoogleTranslateApiImplementation.GoogleTranslateResponse response = translateApi.TranslateGoogleObject(request.Content);
				if (response != null)
				{
					TranslationResponse serviceResponce = new TranslationResponse();
					serviceResponce.OriginalText = request.Content;
					serviceResponce.FromToLanguage = String.Format("{0}-{1}", request.FromLanguage, translateApi.GetTranslateLanguage());
					StringBuilder sb = new StringBuilder();
					foreach (GoogleTranslateApiImplementation.Translation item in response.sentences)
					{
						sb.AppendLine(item.trans);
					}
					serviceResponce.TranslationResult = sb.ToString();
					return serviceResponce;
				}
			}
			catch (Exception ex)
			{
				FaultCode code = new FaultCode(ex.Message);
				FaultReason reason = new FaultReason(string.Format("Exception occurred while translation data {0} {1}", Environment.NewLine, ex.Message));
				FaultException exc = new FaultException(reason, code);
				throw exc;
			}
			return null;
		}

		public VoiceResponse GetVoice(VoiceRequest request)
		{
			if (request == null)
				throw new ArgumentNullException("request");

			VoiceResponse response = new VoiceResponse();
			try
			{
				response.Data = GetVoiceBytes(request.OriginalText, request.ToLanguage);
			}
			catch (Exception ex)
			{
				var fault = new VoiceFault();

				fault.Issue = "Problem connecting to google voice tanslation";
				StringBuilder sb = new StringBuilder();

				sb.AppendLine(ex.GetType().FullName);
				sb.AppendLine(ex.Message);
				if (ex.InnerException != null)
				{
					sb.AppendLine(ex.InnerException.GetType().FullName);
					sb.AppendLine(ex.InnerException.Message);
				}
				fault.Details = sb.ToString();
				sb.Clear();

				throw new FaultException<VoiceFault>(fault);
			}
			return response;
		}

		private byte[] GetVoiceBytes(string translate, string toLang)
		{
			using (xNet.Net.HttpRequest request = new xNet.Net.HttpRequest())
			{
				xNet.Collections.StringDictionary reqParams = new xNet.Collections.StringDictionary();

				//Set neccessary params for request
				reqParams["ie"] = "UTF-8";
				reqParams["q"] = translate;
				reqParams["tl"] = toLang;
				reqParams["prev"] = "input";

				//Get file data
				byte[] bytes = request.Get("http://translate.google.ru/translate_tts", reqParams).ToBytes();

				return bytes;
			}
		}
	}
}
